home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Programming / SWI / source / man / overview.doc (.txt) < prev    next >
Encoding:
LaTeX Document  |  1998-04-15  |  50.3 KB  |  1,010 lines

  1. \chapter{Overview}
  2. \section{Starting SWI-Prolog from the Unix Shell}
  3. It is advised to install SWI-Prolog as `pl' in the local binary
  4. directory. SWI-Prolog can then be started from the Unix shell by typing
  5. `\file{pl}'. The system will boot from the system's default boot
  6. file, perform the necessary initialisations and then enter the
  7. interactive top level.
  8. After the necessary system initialisation the system consults (see
  9. consult/1) the user's initialisation file. This initialisation file
  10. should be named `\file{.plrc}' and reside either in the current
  11. directory or in the user's home directory. If both exist the
  12. initialisation file from the current directory is loaded. The name of
  13. the initialisation file can be changed with the `\argoption{-f}{file}'
  14. option. After loading the initialisation file SWI-Prolog executes a user
  15. initialisation goal. The default goal is a system predicate that prints
  16. the banner message. The default can be modified with the
  17. `\argoption{-g}{goal}' option. Next the toplevel goal is started.
  18. Default is the interactive Prolog loop (see prolog/0). The user can
  19. overwrite this default with the `\argoption{-t}{toplevel}' option.
  20. \subsection{Command Line Options}
  21. The full set of command line options is given below:
  22. \begin{description}
  23.     \cmdlineoptionitem{-help}{}
  24. When given as the only option, it summarises the most important options.
  25.     \cmdlineoptionitem{-v}{}
  26. When given as the only option, it summarises the version and the
  27. architecture identifier.
  28.     \cmdlineoptionitem{-arch}{}
  29. When given as the only option, it prints the architecture identifier
  30. (see feature(arch, Arch)) and exits.
  31.     \cmdlineoptionitem*{-L}{size[km]}
  32. Give local stack limit (2 Mbytes default). Note that there is no space
  33. between the size option and its argument.  By default, the argument is
  34. interpreted in Kbytes.  Postfixing the argument with \const{m} causes
  35. the argument to be interpreted in Mbytes. The following example
  36. specifies 32 Mbytes local stack.
  37. \begin{code}
  38. % pl -L32m
  39. \end{code}
  40. A maximum is useful to stop buggy programs from claiming all memory
  41. resources. \cmdlineoption{-L0} sets the limit to the highest possible
  42. value.
  43.     \cmdlineoptionitem*{-G}{size[km]}
  44. Give global stack limit (4 Mbytes default). See \cmdlineoption{-L} for
  45. more details.
  46.     \cmdlineoptionitem*{-T}{size[km]}
  47. Give trail stack limit (4 Mbytes default). This limit is relatively high
  48. because trail-stack overflows are not often caused program bugs. See
  49. \cmdlineoption{-L} for more details.
  50.     \cmdlineoptionitem*{-A}{size[km]}
  51. Give argument stack limit (1 Mbytes default). The argument stack limits
  52. the maximum nesting of terms that can be compiled and executed. The
  53. SWI-Prolog does `last-argument optimisation' to avoid many deeply nested
  54. structure using this stack. Enlarging this limit is only necessary in
  55. extreme cases. See \cmdlineoption{-L} for more details.
  56.     \cmdlineoptionitem*{-H}{size[km]}
  57. Give \funcref{malloc}{} heap limit. The default is to raise the limit as
  58. high as possible. This option only applies to machines using the
  59. \funcref{mmap}{} function for allocating the Prolog stacks. See
  60. \cmdlineoption{-L} for more details.
  61.     \cmdlineoptionitem{-c}{file \ldots}
  62. Compile files into an `intermediate code file'. See \secref{compilation}.
  63.     \cmdlineoptionitem{-o}{output}
  64. Used in combination with \cmdlineoption{-c} or \cmdlineoption{-b} to
  65. determine output file for compilation.
  66.     \cmdlineoptionitem{-O}{}
  67. Optimised compilation. See please/3.
  68.     \cmdlineoptionitem{-f}{file}
  69. Use \arg{file} as initialisation file instead of `\file{.plrc}'.
  70. `\argoption{-f}{none}' stops SWI-Prolog from searching for an
  71. initialisation file.
  72.     \cmdlineoptionitem{-F}{script}
  73. Selects a startup-script from the SWI-Prolog home directory. The
  74. script-file is named \file{<script>.rc}. The default
  75. \arg{script} name is deduced from the executable, taking the leading
  76. alphanumerical characters (letters, digits and underscore) from the
  77. program-name. \argoption{-F}{none} stops looking for a script. Intended
  78. for simple management of slightly different versions. One could for
  79. example write a script \file{iso.rc} and then select ISO compatibility
  80. mode using \exam{pl -F iso} or make a link from \program{iso-pl} to
  81. \program{pl}.
  82.     \cmdlineoptionitem{-g}{goal}
  83. \arg{Goal} is executed just before entering the top level. Default is a
  84. predicate which prints the welcome message. The welcome message can
  85. thus be suppressed by giving \argoption{-g}{true}. \arg{goal} can be a complex
  86. term. In this case quotes are normally needed to protect it from
  87. being expanded by the Unix shell. 
  88.     \cmdlineoptionitem{-t}{goal}
  89. Use \arg{goal} as interactive toplevel instead of the default goal
  90. prolog/0. \arg{goal} can be a complex term. If the toplevel goal
  91. succeeds SWI-Prolog exits with status 0. If it fails the exit status is 1.
  92. This flag also determines the goal started by break/0 and abort/0. If
  93. you want to stop the user from entering interactive mode start the
  94. application with `\argoption{-g}{goal}' and give `halt' as toplevel.
  95.     \cmdlineoptionitem{-tty}{}
  96. Switches tty control (using ioctl(2)) on (\cmdlineoption{+tty}) or off
  97. (\cmdlineoption{-tty}). Normally tty control is switched on. This default
  98. depends on the installation.  You may wish to switch tty control off if
  99. Prolog is used from an editor such as Emacs.  If switched off
  100. get_single_char/1 and the tracer will wait for a return.
  101.     \cmdlineoptionitem{-x}{bootfile}
  102. Boot from \arg{bootfile} instead of the system's default boot file. A
  103. bootfile is a file resulting from a Prolog compilation using the
  104. \cmdlineoption{-b} or \cmdlineoption{-c} option or a program saved using
  105. qsave_program/[1,2].
  106.     \cmdlineoptionitem{-r}{restorefile}
  107. Restore a state created by save_program/[1,2] or save/[1,2] using
  108. the new-style saved-states.  Equivalent to \exam{restore(restorefile)} from
  109. Prolog.
  110.     \cmdlineoptionitem{-p}{alias=path1[:path2 \ldots]}
  111. Define a path alias for file_search_path. \arg{alias} is the name of
  112. the alias, \arg{path1 ...} is a \chr{:} separated list of values for
  113. the alias.  A value is either a term of the form \mbox{alias(value)}
  114. or pathname.  The computed aliases are added to file_search_path/2
  115. using asserta/1, so they precede predefined values for the alias.  See
  116. file_search_path/2 for details on using this file-location mechanism.
  117.     \cmdlineoptionitem{{\tt --}}{}
  118. Stops scanning for more arguments, so you can pass arguments for your
  119. application after this one.
  120. \end{description}
  121. The following options are for system maintenance. They are given
  122. for reference only.
  123. \begin{description}
  124.     \cmdlineoptionitem{-b}{initfile \ldots \cmdlineoption{-c} file \ldots}
  125. Boot compilation.  \arg{initfile \ldots} are compiled by the C-written
  126. bootstrap compiler, \arg{file \ldots} by the normal Prolog compiler.
  127. System maintenance only.
  128.     \cmdlineoptionitem{-d}{level}
  129. Set debug level to \arg{level}. Only has effect if the system is
  130. compiled with the \const{-DO_DEBUG} flag. System maintenance
  131. only.
  132. \end{description}
  133. \section{GNU Emacs Interface}
  134. \index{GNU-Emacs}\index{Emacs}
  135. A provisional interface to \program{emacs} has been included since
  136. version 1.6 of SWI-Prolog. The interface is based on the freely
  137. distributed interface delivered with Quintus Prolog. When running Prolog
  138. as an inferior process under GNU-Emacs, there is support for finding
  139. predicate definitions, completing atoms, finding the locations of
  140. compilation-warnings and many more. For details, see the files
  141. \file{pl/lisp/README} and \file{pl/lisp/swi-prolog.el}. 
  142. \section{Online Help}
  143. Online help provides a fast lookup and browsing facility to this
  144. manual. The online manual can show predicate definitions as well as
  145. entire sections of the manual.
  146. The online help is displayed from the file \pllib{'MANUAL'}. The file
  147. \pllib{helpidx} provides an index into this file. \pllib{'MANUAL'} is
  148. created from the \LaTeX{} sources with a modified version of
  149. \program{dvitty}, using overstrike for printing bold text and
  150. underlining for rendering italic text. XPCE is shipped with
  151. \pllib{swi_help}, presenting the information from the online help in a
  152. hypertext window. The feature \const{write_help_with_overstrike}
  153. controls whether or not help/1 writes its output using overstrike to
  154. realise bold and underlined output or not. If this feature is not set it
  155. is initialised by the help library to \const{true} if the \const{TERM}
  156. variable equals \const{xterm} and \const{false} otherwise. If this
  157. default does not satisfy you, add the following line to \file{~/.plrc}.
  158. \begin{code}
  159. :- set_feature(write_help_with_overstrike, true).
  160. \end{code}
  161. \begin{description}
  162.     \predicate{help}{0}{}
  163. Equivalent to \exam{help(help/1)}.
  164.     \predicate{help}{1}{+What}
  165. Show specified part of the manual.  \arg{What} is one of:
  166. \begin{center}\begin{tabular}{lp{3.5in}}
  167. <Name>/<Arity>  & give help on specified predicate \\
  168. <Name>          & give help on named predicate with any arity
  169.                   or C interface function with that name \\
  170. <Section>       & display specified section. section numbers are
  171.                   dash-separated numbers: \exam{2-3} refers to
  172.                   section 2.3 of the manual.  Section numbers are
  173.                   obtained using apropos/1.
  174. \end{tabular}\end{center}
  175. Examples
  176. \begin{center}\begin{tabular}{lp{3.5in}}
  177. \exam{?- help(assert).}    & give help on predicate assert \\
  178. \exam{?- help(3-4).}       & display section 3.4 of the manual \\
  179. \exam{?- help('PL_retry').}& give help on interface function PL_retry() \\
  180. \end{tabular}\end{center}
  181.     \predicate{apropos}{1}{+Pattern}
  182. Display all predicates, functions and sections that have {\em
  183. Pattern} in their name or summary description.  Lowercase letters in
  184. \arg{Pattern} also match a corresponding uppercase letter. Example:
  185. \begin{center}\begin{tabular}{lp{3.5in}}
  186. \exam{?- apropos(file).}   & Display predicates, functions and sections
  187.                             that have `file' (or `File', etc.) in their
  188.                             summary description. \\
  189. \end{tabular}\end{center}
  190.     \predicate{explain}{1}{+ToExplain}
  191. Give an explanation on the given `object'.  The argument may be
  192. any Prolog data object.  If the argument is an atom, a term of
  193. the form \arg{Name/Arity} or a term of the form {\em
  194. Module:Name/Arity}, explain will try to explain the predicate
  195. as well as possible references to it.
  196.     \predicate{explain}{2}{+ToExplain, -Explanation}
  197. Unify \arg{Explanation} with an explanation for \arg{ToExplain}.
  198. Backtracking yields further explanations.
  199. \end{description}
  200. \section{Query Substitutions}
  201. \label{sec:history}
  202. SWI-Prolog offers a query substitution mechanism similar to that of Unix
  203. csh (csh(1)), called `history'. The availability of this feature is
  204. controlled by set_feature/2, using the \const{history} feature. By
  205. default, history is available if the feature \const{readline} is
  206. \const{false}. To enable this feature, remembering the last 50 commands,
  207. put the following into your \file{~/.plrc} file:
  208. \begin{code}
  209. :- set_feature(history, 50).
  210. \end{code}
  211. The history system allows the user to compose new queries from those
  212. typed before and remembered by the system. It also allows to correct
  213. queries and syntax errors. SWI-Prolog does not offer the Unix csh
  214. capabilities to include arguments. This is omitted as it is unclear how
  215. the first, second, etc.\ argument should be defined.%
  216.     \footnote{One could choose words, defining words as a sequence of
  217.               alpha-numeric characters and the word separators as
  218.               anything else, but one could also choose Prolog
  219.               arguments}
  220. The available history commands are shown in \tabref{history}.
  221. \Figref{history} gives some examples.
  222. \begin{table}
  223. \begin{center}
  224. \begin{tabular}{|l|l|}
  225. \hline
  226. \verb+!!.+            & Repeat last query \\
  227. \verb+!nr.+           & Repeat query numbered <nr> \\
  228. \verb+!str.+          & Repeat last query starting with <str> \\
  229. \verb+!?str.+         & Repeat last query holding <str> \\
  230. \verb+^old^new.+      & Substitute <old> into <new> of
  231.                         last query \\
  232. \verb+!nr^old^new.+   & Substitute in query numbered <nr> \\
  233. \verb+!str^old^new.+  & Substitute in query starting with <str> \\
  234. \verb+!?str^old^new.+ & Substitute in query holding <str> \\
  235. \verb+h.+             & Show history list \\
  236. \verb+!h.+            & Show this list \\
  237. \hline
  238. \end{tabular}
  239. \end{center}
  240.     \caption{History commands}
  241.     \label{tab:history}
  242. \end{table}
  243. \begin{figure}
  244. \begin{code}
  245. /staff/jan/.plrc consulted, 0.066667 seconds, 591 bytes
  246. Welcome to SWI-Prolog (Version \plversion)
  247. Copyright (c) 1993-1996 University of Amsterdam.  All rights reserved.
  248. For help, use ?- help(Topic). or ?- apropos(Word).
  249. 1 ?- append("Hello ", "World", L).
  250. L = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] 
  251. 2 ?- !!, writef('L = %s\n', [L]).
  252. append("Hello ", "World", L), writef('L = %s\n', [L]).
  253. L = Hello World
  254. L = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100] 
  255. 3 ?- sublist(integer, [3, f, 3.4], L).
  256. L = [3] 
  257. 4 ?- ^integer^number.
  258. sublist(number, [3, f, 3.4], L).
  259. L = [3, 3.400000] 
  260. 5 ?- h.
  261.     1   append("Hello ", "World", L).
  262.     2   append("Hello ", "World", L), writef('L = %s\n', [L]).
  263.     3   sublist(integer, [3, f, 3.4], L).
  264.     4   sublist(number, [3, f, 3.4], L).
  265. 5 ?- !2^World^Universe.
  266. append("Hello ", "Universe", L), writef('L = %s\n', [L]).
  267. L = Hello Universe
  268. L = [72, 101, 108, 108, 111, 32, 85, 110, 105, 118, 101, 114, 115, 101] 
  269. 6 ?- halt.
  270. \end{code}
  271.     \caption{Some examples of the history facility}
  272.     \label{fig:history}
  273. \end{figure}
  274. \subsection{Limitations of the History System}
  275. When in top level SWI-Prolog reads the user's queries using
  276. read_history/6 rather than read/1.  This predicate first reads the
  277. current input stream up to a full stop.  While doing so it maps all
  278. contiguous blank space onto a single space and deletes
  279. \exam{/* \ldots */} and \exam{\% \ldots <cr>} comments.  Parts between double quotes
  280. (\const{"}) or single quotes (\const{'}) are left unaltered.  Note that a
  281. Prolog full stop consists of a `non-symbol' character, followed by a
  282. period (\const{.}), followed by a blank character.  `Symbol' characters
  283. are: \verb!#$&*+-./:<=>?@^`~!. A single quote immediately preceded by
  284. a digit (0-9) is considered part of the \mbox{<digit>'<digit>\ldots}
  285. (e.g.\ \exam{2'101}; binary number 101) sequence.
  286. After this initial parsing the result is first checked for the special
  287. \mbox{\Shat<old>\Shat<new>.} construction. If this fails the
  288. string is checked for all occurrences of the \const{!}, followed by a
  289. \const{!}, \const{?}, a digit, a letter or an underscore. These special
  290. sequences are analysed and the appropriate substitution from the history
  291. list is made.
  292. From the above it follows that it is hard or impossible to
  293. correct quotation with single or double quotes, comment delimiters and
  294. spacing.
  295. \section{Reuse of toplevel bindings}    \label{sec:topvars}
  296. Bindings resulting from the successful execution of a toplevel goal
  297. are asserted in a database.  These values may be reused in further
  298. toplevel queries as \$Var.  Only the latest binding is available.
  299. Example:
  300. \begin{figure}
  301. \begin{code}
  302. 1 ?- maplist(plus(1), "hello", X).
  303. X = [105,102,109,109,112] 
  304. 2 ?- format('~s~n', [$X]).
  305. ifmmp
  306. \end{code}
  307.     \caption{Reusing toplevel bindings}
  308.     \label{fig:topevelvars}
  309. \end{figure} 
  310. Note that variables may be set by executing \predref{=}{2}:
  311. \begin{code}
  312. 6 ?- X = statistics.
  313. X = statistics
  314. 7 ?- $X.
  315. 28.00 seconds cpu time for 183,128 inferences
  316. 4,016 atoms, 1,904 functors, 2,042 predicates, 52 modules
  317. 55,915 byte codes; 11,239 external references
  318.                       Limit    Allocated       In use
  319. Heap         :                                624,820 Bytes
  320. Local  stack :    2,048,000        8,192          404 Bytes
  321. Global stack :    4,096,000       16,384          968 Bytes
  322. Trail  stack :    4,096,000        8,192          432 Bytes
  323. \end{code}
  324. \section{Overview of the Debugger}
  325. SWI-Prolog has a 6-port tracer, extending the standard 4-port tracer
  326. \cite{Clocksin:87} with two additional ports. The optional \arg{unify}
  327. port allows the user to inspect the result after unification of the
  328. head.  The \arg{exception} port shows exceptions raised by throw/1
  329. or one of the built-in predicates.  See \secref{exception}.
  330. The standard ports are called \const{call}, \const{exit}, \const{redo},
  331. \const{fail} and \const{unify}. The tracer is started by the trace/0
  332. command, when a spy point is reached and the system is in debugging mode
  333. (see spy/1 and debug/0) or when an exception is raised.
  334. The interactive toplevel goal trace/0 means ``trace the next query''.
  335. The tracer shows the port, displaying the port name, the current depth
  336. of the recursion and the goal. The goal is printed using the Prolog
  337. predicate print/1 (default), write/1 or display/1. An example using all
  338. ports is shown in \figref{tracer}.
  339. \begin{figure}
  340. \begin{code}
  341. 2 ?- visible(+all), leash(-exit).
  342. 3 ?- trace, min([3, 2], X).
  343.   Call:  ( 3) min([3, 2], G235) ? creep
  344.   Unify: ( 3) min([3, 2], G235)
  345.   Call:  ( 4) min([2], G244) ? creep
  346.   Unify: ( 4) min([2], 2)
  347.   Exit:  ( 4) min([2], 2)
  348.   Call:  ( 4) min(3, 2, G235) ? creep
  349.   Unify: ( 4) min(3, 2, G235)
  350.   Call:  ( 5) 3 < 2 ? creep
  351.   Fail:  ( 5) 3 < 2 ? creep
  352.   Redo:  ( 4) min(3, 2, G235) ? creep
  353.   Exit:  ( 4) min(3, 2, 2)
  354.   Exit:  ( 3) min([3, 2], 2)
  355. \end{code}
  356.     \caption{Example trace}
  357.     \label{fig:tracer}
  358. \end{figure}
  359. On {\em leashed ports} (set with the predicate leash/1, default are
  360. \const{call}, \const{exit}, \const{redo} and \const{fail}) the user is
  361. prompted for an action. All actions are single character commands which
  362. are executed {\bf without} waiting for a return, unless the command line
  363. option \cmdlineoption{-tty} is active. Tracer options:
  364. \begin{description}
  365.     \traceoption{+}{Spy}{
  366. Set a spy point (see spy/1) on the current predicate.} 
  367.     \traceoption{-}{No spy}{
  368. Remove the spy point (see nospy/1) from the current predicate.} 
  369.     \traceoption{/}{Find}{
  370. Search for a port.  After the `/', the user can enter a line
  371. to specify the port to search for.  This line consists of a set of
  372. letters indicating the port type, followed by an optional term,
  373. that should unify with the goal run by the port.  If no term is
  374. specified it is taken as a variable, searching for any port of the
  375. specified type.  If an atom is given, any goal whose functor has a
  376. name equal to that atom matches.  Examples:
  377. \begin{center}\begin{tabular}{lp{3in}}
  378. \tt /f                  & Search for any fail port \\
  379. \tt /fe solve           & Search for a fail or exit port of any goal with
  380.                           name \const{solve} \\
  381. \tt /c solve(a, _)      & Search for a call to {solve}/2 whose first argument
  382.                           is a variable or the atom \const{a} \\
  383. \tt /a member(_, _)     & Search for any port on member/2. This is equivalent
  384.                           to setting a spy point on member/2. \\
  385. \end{tabular}\end{center}}
  386.     \traceoption{.}{Repeat find}{
  387. Repeat the last find command (see `/')}
  388.     \traceoption{A}{Alternatives}{
  389. Show all goals that have alternatives.} 
  390.     \traceoption{C}{Context}{
  391. Toggle `Show Context'. If \const{on} the context module of the goal is
  392. displayed between square brackets (see \secref{modules}).
  393. Default is \const{off}.} 
  394.     \traceoption{L}{Listing}{
  395. List the current predicate with listing/1.} 
  396.     \traceoption{a}{Abort}{
  397. Abort Prolog execution (see abort/0).} 
  398.     \traceoption{b}{Break}{
  399. Enter a Prolog break environment (see break/0).} 
  400.     \traceoption{c}{Creep}{
  401. Continue execution, stop at next port. (Also return, space).} 
  402.     \traceoption{d}{Display}{
  403. Write goals using the Prolog predicate display/1.} 
  404.     \traceoption{e}{Exit}{
  405. Terminate Prolog (see halt/0).} 
  406.     \traceoption{f}{Fail}{
  407. Force failure of the current goal} 
  408.     \traceoption{g}{Goals}{
  409. Show the list of parent goals (the execution stack). Note that due to tail
  410. recursion optimization a number of parent goals might not exist any more.} 
  411.     \traceoption{h}{Help}{
  412. Show available options (also `?').} 
  413.     \traceoption{i}{Ignore}{
  414. Ignore the current goal, pretending it succeeded.} 
  415.     \traceoption{l}{Leap}{
  416. Continue execution, stop at next spy point.} 
  417.     \traceoption{n}{No debug}{
  418. Continue execution in `no debug' mode.} 
  419.     \traceoption{p}{Print}{
  420. Write goals using the Prolog predicate print/1 (default).} 
  421.     \traceoption{r}{Retry}{
  422. Undo all actions (except for database and i/o actions) back to the call
  423. port of the current goal and resume execution at the call port.} 
  424.     \traceoption{s}{Skip}{
  425. Continue execution, stop at the next port of {\bf this} goal (thus skipping
  426. all calls to children of this goal).} 
  427.     \traceoption{u}{Up}{
  428. Continue execution, stop at the next port of {\bf the parent} goal (thus
  429. skipping this goal and all calls to children of this goal). This option
  430. is useful to stop tracing a failure driven loop.} 
  431.     \traceoption{w}{Write}{
  432. Write goals using the Prolog predicate write/1.} 
  433. \end{description}
  434. The ideal 4 port model as described in many Prolog books
  435. \cite{Clocksin:87} is not visible in many Prolog implementations because
  436. code optimisation removes part of the choice- and exit points.
  437. Backtrack points are not shown if either the goal succeeded
  438. deterministically or its alternatives were removed using the cut.  When
  439. running in debug mode (debug/0) choice points are only destroyed when
  440. removed by the cut.  In debug mode, tail recursion optimisation is
  441. switched off.%
  442.     \footnote{This implies the system can run out of local stack in debug
  443.               mode, while no problems arise when running in non-debug mode.}
  444. \section{Compilation}            \label{sec:compilation}
  445. \subsection{During program development}
  446. During program development, programs are normally loaded using
  447. consult/1, or the list abreviation. It is common practice to organise a
  448. project as a collection of source-files and a \jargon{load-file}, a
  449. Prolog file containing only use_module/[1,2] or ensure_loaded/1
  450. directives, possibly with a definition of the \jargon{entry-point} of
  451. the program, the predicate that is normally used to start the program.
  452. This file is often called \file{load.pl}.  If the entry-point is called
  453. {\em go}, a typical session starts as:
  454. \begin{code}
  455. <banner>
  456. 1 ?- [load].
  457. <compilation messages>
  458. 2 ?- go.
  459. <program interaction>
  460. \end{code}
  461. When using Windows, the user may open \file{load.pl} from the Windows
  462. explorer, which will cause \program{plwin.exe} to be started in the
  463. directory holding \file{load.pl}.  Prolog loads \file{load.pl} before
  464. entering the toplevel.
  465. \subsection{For running the result}
  466. There are various options if you want to make your program ready
  467. for real usage.  The best choice depends on whether the program
  468. is to be used only on machines holding the SWI-Prolog development
  469. system, the size of the program and the operating system (Unix
  470. vs.\ Windows).
  471. \subsubsection{Creating a shell-script}
  472. Especially on Unix systems and not-too-large applications, writing
  473. a shell-script that simply loads your application and calls the
  474. entry-point is often a good choice.  A skeleton for the script is
  475. given below, followed by the Prolog code to obtain the program
  476. arguments.
  477. \begin{code}
  478. #!/bin/sh
  479. base=<absolute-path-to-source>
  480. PL=pl
  481. exec $PL -f none -g "load_files(['$base/load'],[silent(true)])" -t go -- $*
  482. \end{code}
  483. \begin{code}
  484. go :-
  485.     unix(argv(Arguments)),
  486.     append(_SytemArgs, [--|Args], Arguments), !,
  487.     go(Args).
  488. go(Args) :-
  489. \end{code}
  490. On Windows systems, similar behaviour can be achieved by creating a 
  491. shortcut to Prolog, passing the proper options or writing a \fileext{bat}
  492. file.
  493. \subsubsection{Creating a saved-state}
  494. For larger programs, as well as for programs that are required run on
  495. systems that do not have the SWI-Prolog development system installed,
  496. creating a saved state is the best solution. A saved state is created
  497. using qsave_program/[1,2] or using the linker plld(1). A saved state is
  498. a file containing machine-independent intermediate code in a format
  499. dedicated for fast loading. Optionally, the emulator may be integrated
  500. in the saved state, creating a single-file, but machine-dependent,
  501. exectable.  This process is descriped in \chapref{runtime}.
  502. \subsubsection{Compililation using the -c commandline option}
  503. This mechanism is mostly for backward compatibility. It creates files in
  504. the same format as saved-states created using qsave_program/[1,2], but
  505. the resulting file is a translation of the source-files read, rather
  506. than a translation of the state of the machine.   Unlike saved states,
  507. programs created using \argoption{-c}{file} do not include the Prolog
  508. part of the development system.  The result is very dependent on the
  509. local SWI-Prolog installation.
  510. The command below is used to compile one or more source-files.
  511. \begin{code}
  512. pl [options] [-o output] -c file ...
  513. \end{code}
  514. The individual source files may include other files using the standard
  515. list notation, consult/1, ensure_loaded/1 and use_module/[1,2]. When the
  516. \argoption{-o}{file} option is omitted a file named \file{a.out} is
  517. created that holds the intermediate code file.
  518. Intermediate code files start with the Unix magic code \const{#!}
  519. and are executable. This implies they can be started as a command:
  520. \begin{code}
  521. % pl -o my_program -c ...
  522. % my_program [options]
  523. \end{code}
  524. Alternatively, {\tt my_program} can be started with
  525. \begin{code}
  526. % pl -x my_program [options]
  527. \end{code}
  528. The following restrictions apply to source files that are to be
  529. compiled with `\cmdlineoption{-c}':
  530. \begin{itemize}
  531.     \item
  532. term_expansion/2 should not use assert/1 and or retract/1 other than
  533. for local computational purposes.
  534.     \item
  535. Files can only be included by the standard include directives: [...],
  536. consult/1, ensure_loaded/1 and use_module/[1,2].  User defined loading
  537. predicate invocations will not be compiled.
  538. \end{itemize}
  539. Directives are executed both when compiling the program and when
  540. loading the intermediate code file.
  541. \section{Environment Control}
  542. The current system defines 3 different mechanisms to query and/or set
  543. properties of the environment: please/3, flag/3 and feature/2 as well as
  544. a number of special purpose predicates of which unknown/2, fileerrors/2
  545. are examples.  The ISO standard defines prolog_flag.  It is likely that
  546. all these global features will be merged into a single in the future.
  547. \begin{description}
  548.     \predicate{please}{3}{+Key, -Old, +New}
  549. The predicate please/3%
  550.     \footnote{The idea comes from BIM_Prolog. The options supported by
  551.               this predicate are not compatible with those for
  552.               BIM_Prolog however.}
  553. is a solution to avoid large numbers of environment control
  554. predicates. Later versions will support other environment control as
  555. now provided via the predicates style_check/1, leash/1, unknown/2, the
  556. tracer predicates, etc. These predicates are then moved into a library
  557. for backwards compatibility. The currently available options are:
  558. \begin{description}
  559.     \pleaseoption{optimise}{on/off}{off}
  560. Switch optimise mode for the compiler \const{on} or \const{off} (see also
  561. the command line option \cmdlineoption{-O}). Currently optimise compilation only
  562. implies compilation of arithmetic, making it fast, but invisible to the
  563. tracer.  Later versions might imply various other optimisations such as
  564. incorporating a number of basic predicates in the virtual machine
  565. (var/1, fail/0, =/2, etc.)  to gain speed at the cost of crippling the
  566. debugger.  Also source level optimisations such as integrating small
  567. predicates into their callers, eliminating constant expressions and
  568. other predictable constructs.  Source code optimisation is never
  569. applied to predicates that are declared dynamic (see dynamic/1).
  570.     \pleaseoption{autoload}{on/off}{on}
  571. If \const{on} autoloading of library functions is enabled. If
  572. \const{off} autoloading is disabled. See \secref{autoload}.
  573.     \pleaseoption{verbose_autoload}{on/off}{off}
  574. If \const{on} the normal consult message will be printed if a library is
  575. autoloaded.  By default this message is suppressed.  Intended to be used
  576. for debugging purposes (e.g.\ where does this predicate come from?).
  577. \end{description}
  578.     \predicate{feature}{2}{?Key, -Value}
  579. The predicate feature/2 defines an interface to installation features:
  580. options compiled in, version, home, etc.  With both arguments unbound,
  581. it will generate all defined features.  With the `Key' instantiated it
  582. unify the value of the feature.  Features come in three types: boolean
  583. features, features with an atom value and features with an integer
  584. value. A boolean feature is true iff the feature is present {\bf and}
  585. the \arg{Value} is the atom \const{true}. Currently defined keys:
  586. \begin{description}
  587.     \featureoption{arch}{atom}
  588. Identifier for the hardware and operating system SWI-Prolog is running
  589. on.  Used to determine the startup file as well as to select foreign
  590. files for the right architecture.  See also load_foreign/5.
  591.     \featureoption{version}{integer}
  592. The version identifier is an integer with value: $$10000 \times
  593. \arg{Major} + 100 \times \arg{Minor} + \arg{Patch}$$
  594. Note that in releases upto 2.7.10 this feature yielded an atom holding
  595. the three numbers separated by dots. The current representation is much
  596. easier for implementing version-conditional statements.
  597.     \featureoption{home}{atom}
  598. SWI-Prolog's notion of the home-directory.  SWI-Prolog uses it's home
  599. directory to find its startup file as \file{<home>/startup/startup.<arch>}
  600. and to find its library as \file{<home>/library}.
  601.     \featureoption{pipe}{bool}
  602. If true, tell(pipe(command)), etc.\ are supported.
  603.     \featureoption{load_foreign}{bool}
  604. If true, load_foreign/[2,5] are implemented.
  605.     \featureoption{open_shared_object}{bool}
  606. If true, open_shared_object/2 and friends are implemented, providing
  607. access to shared libraries (.so files).  This requires the C-library
  608. functions dlopen() and friends as well as the configuration option
  609. {\tt --with-dlopen}.
  610.     \featureoption{dynamic_stacks}{bool}
  611. If true, the system uses some form of `sparse-memory management' to
  612. realise the stacks.  If false, malloc()/realloc() are used for the stacks.
  613. In earlier days this had consequenses for foreign code.  As of version
  614. 2.5, this is no longer the case.
  615. Systems using `sparse-memory management' are a bit faster as there is no
  616. stack-shifter, and checking the stack-boundary is often realised by the
  617. hardware using a `guard-page'. Also, memory is actually returned to the
  618. system after a garbage collection or call to trim_stacks/0 (called by
  619. prolog/0 after finishing a user-query).
  620.     \featureoption{c_libs}{atom}
  621. Libraries passed to the C-linker when SWI-Prolog was linked.  May be used
  622. to determine the libraries needed to create statically linked extensions
  623. for SWI-Prolog.  See \secref{plld}.
  624.     \featureoption{c_staticlibs}{atom}
  625. On some machines, the SWI-Prolog executable is dynamically linked, but
  626. requires some libraries to be statically linked.  Obsolete.
  627.     \featureoption{c_cc}{atom}
  628. Name of the C-compiler used to compile SWI-Prolog.  Normally either gcc
  629. or cc.  See \secref{plld}.
  630.     \featureoption{c_ldflags}{atom}
  631. Special linker flags passed to link SWI-Prolog.  See \secref{plld}.
  632.     \featureoption{save}{bool}
  633. If true, save/[1,2] is implemented.  Saving using save/0 is obsolete.  See
  634. qsave_program/[1,2].
  635.     \featureoption{save_program}{bool}
  636. If true, save_program/[1,2] is implemented.  Saving using save_program/0
  637. is obsolete.  See qsave_program/[1,2].
  638.     \featureoption{readline}{bool}
  639. If true, SWI-Prolog is linked with the readline library.  This is done
  640. by default if you have this library installed on your system.  It is
  641. also true for the Win32 plwin.exe version of SWI-Prolog, which realises
  642. a subset of the readline functionality.
  643.     \featureoption{saved_program}{bool}
  644. If true, Prolog is started from a state saved with qsave_program/[1,2].
  645.     \featureoption{runtime}{bool}
  646. If true, SWI-Prolog is compiled with -DO_RUNTIME, disabling various
  647. useful development features (currently the tracer and profiler).
  648.     \featureoption{max_integer}{integer}
  649. Maximum integer value.  Most arithmetic operations will automatically
  650. convert to floats if integer values above this are returned.
  651.     \featureoption{min_integer}{integer}
  652. Minimum integer value.
  653.     \featureoption{max_tagged_integer}{integer}
  654. Maximum integer value represented as a `tagged' value.  Tagged integers
  655. require 4-bytes storage and are used for indexing.  Larger integers are
  656. represented as `indirect data' and require 16-bytes on the stacks (though
  657. a copy requires only 4 additional bytes).
  658.     \featureoption{min_tagged_integer}{integer}
  659. Start of the tagged-integer value range.
  660.     \featureoption{float_format}{atom}
  661. C {\tt printf()} format specification used by write/1 and friends to
  662. determine how floating point numbers are printed.  The default is {\tt
  663. \%g}.  May be changed.  The specified value is passed to printf()
  664. without further checking.  For example, if you want more digits printed,
  665. {\tt \%.12g} will print all floats using 12 digits instead of the
  666. default 6.  See also format/[1,2], write/1, print/1 and portray/1.
  667.     \featureoption{compiled_at}{atom}
  668. Describes when the system has been compiled. Only available if the
  669. C-compiler used to compile SWI-Prolog provides the __DATE__ and __TIME__
  670. macros.
  671.     \featureoption{character_escapes}{bool}
  672. If true (default), read/1 interprets \verb$\$ escape sequences in quoted
  673. atoms and strings.  May be changed.
  674.     \featureoption{allow_variable_name_as_functor}{bool}
  675. If true (default is false), \exam{Functor(arg)} is read as if it was
  676. written \exam{'Functor'(arg)}. Some applications use the Prolog read/1
  677. predicate for reading an application defined script language. In these
  678. cases, it is often difficult to explain none-Prolog users of the
  679. application that constants and functions can only start with a lowercase
  680. letter. Variables can be turned into atoms starting with an uppercase
  681. atom by calling read_term/2 using the option \const{variable_names} and
  682. binding the variables to their name. Using this feature, F(x) can be
  683. turned into valid syntax for such script languages. Suggested by Robert
  684. van Engelen.  SWI-Prolog specific.
  685.     \featureoption{history}{integer}
  686. If $> 0$, support Unix \program{csh(1)} like history as described in
  687. \secref{history}.  Otherwise, only support reusing commands through
  688. the commandline editor.  The default is to set this feature to 0 if
  689. a commandline editor is provided (see feature \const{readline}) and
  690. 15 otherwise.
  691.     \featureoption{gc}{bool}
  692. If true (default), the garbage collector is active.  If false, neither
  693. garbage-collection, nor stack-shifts will take place, even not on
  694. explicit request.  May be changed.
  695.     \featureoption{trace_gc}{bool}
  696. If true (false is the default), garbage collections and stack-shifts
  697. will be reported on the terminal.  May be changed.
  698.     \featureoption{max_arity}{unbounded}
  699. ISO feature describing there is no maximum arity to compound terms.
  700.     \featureoption{integer_rounding_function}{down,toward_zero}
  701. ISO feature describing rounding by \verb$//$ and \verb$rem$ arithmetic
  702. functions. Value depends on the C-compiler used.
  703.     \featureoption{bounded}{true}
  704. ISO feature describing integer representation is bound by
  705. {\tt min_integer} and {\tt min_integer}.
  706.     \featureoption{tty_control}{bool}
  707. Determines whether the terminal is switched to raw mode for
  708. get_single_char/1, which also reads the user-actions for the trace.  May
  709. be set.  See also the \cmdlineoption{+/-tty} command-line option.
  710.     \featureoption{debug_on_error}{bool}
  711. If {\tt true}, start the tracer after an error is detected.  Otherwise
  712. just continue execution.  The goal that raised the error will normally
  713. fail.  See also fileerrors/2 and the feature {\tt report_error}.  May
  714. be changed.  Default is {\tt true}, except for the runtime version.
  715.     \featureoption{report_error}{bool}
  716. If {\tt true}, print error messages, otherwise suppress them.  May be
  717. changed.  See also the {\tt debug_on_error} feature.  Default is {\tt
  718. true}, except for the runtime version.  
  719.     \featureoption{unix}{bool}
  720. \index{unix}%
  721. If {\tt true}, the operating system is some version of Unix.  Defined
  722. if the C-compiler used to compile this version of SWI-Prolog either
  723. defines \verb$__unix__$ or \const{unix}.
  724.     \featureoption{windows}{bool}
  725. \index{windows}%
  726. If {\tt true}, the operating system is an implementation of Microsoft
  727. Windows (3.1, 95, NT, etc.).
  728. \end{description}
  729.     \predicate{set_feature}{2}{+Key, +Value}
  730. Define a new feature or change its value.  \arg{Key} is an atom,
  731. \arg{Value} is an atom or number.
  732. \end{description}
  733. \section{Automatic loading of libraries}    \label{sec:autoload}
  734. If ---at runtime--- an undefined predicate is trapped the system will
  735. first try to import the predicate from the module's default module.
  736. If this fails the \jargon{auto loader} is activated.  On first activation
  737. an index to all library files in all library directories is loaded in
  738. core (see library_directory/1).  If the undefined predicate can be
  739. located in the one of the libraries that library file is automatically
  740. loaded and the call to the (previously undefined) predicate is
  741. resumed.  By default this mechanism loads the file silently.  The
  742. please/3 option \const{verbose_autoload} is provided to get verbose
  743. loading. The please option \const{autoload} can be used to
  744. enable/disable the entire auto load system.
  745. Autoloading only handles (library) source files that use the module
  746. mechanism described in \chapref{modules}.  The files are loaded
  747. with use_module/2 and only the trapped undefined predicate will be imported
  748. to the module where the undefined predicate was called.  Each library
  749. directory must hold a file \file{INDEX.pl} that contains an index to all
  750. library files in the directory.  This file consists of lines of the 
  751. following format:
  752. \begin{code}
  753. index(Name, Arity, Module, File).
  754. \end{code}
  755. The predicate make/0 scans the autoload libraries and updates the
  756. index if it exists, is writable and out-of-date.  It is advised to
  757. create an empty file called \file{INDEX.pl} in a library directory
  758. meant for auto loading before doing anything else.  This index file
  759. can then be updated by running the prolog make_library_index/1 (`\%' is
  760. the Unix prompt):
  761. \begin{code}
  762. % mkdir ~/lib/prolog
  763. % cd !$
  764. % pl -g true -t 'make_library_index(.)'
  765. \end{code}
  766. If there are more than one library files containing the desired predicate
  767. the following search schema is followed:
  768. \begin{enumerate}
  769.   \item If a there is a library file that defines the module in which
  770.         the undefined predicate is trapped, this file is used.
  771.   \item Otherwise library files are considered in the order they appear
  772.         in the library_directory/1 predicate and within the directory
  773.         alphabetically.
  774. \end{enumerate}
  775. \begin{description}
  776.     \predicate{make_library_index}{1}{+Directory}
  777. Create an index for this directory.  The index is written to the file
  778. 'INDEX.pl' in the specified directory.  Fails with a warning if the
  779. directory does not exist or is write protected.
  780. \end{description}
  781. \subsection{Notes on Automatic Loading}
  782. The autoloader is a new feature to SWI-Prolog.  Its aim is to simplify
  783. program development and program management.  Common lisp has a similar
  784. feature, but here the user has to specify which library is to be
  785. loaded if a specific function is called which is not defined.  The
  786. advantage of the SWI-Prolog schema is that the user does not have to
  787. specify this.  The disadvantage however is that the user might be
  788. wondering ``where the hell this predicate comes from''.  Only
  789. experience can learn whether the functionality of the autoloader is
  790. appropriate.  Comments are welcome.
  791. The autoloader only works if the unknown flag (see unknown/2) is set to
  792. \const{trace} (default).  A more appropriate interaction with this flag
  793. will be considered.
  794. \section{Garbage Collection}
  795. SWI-Prolog version 1.4 was the first release to support garbage
  796. collection.  Together with tail-recursion optimisation this guaranties
  797. forward chaining programs do not waste indefinite amounts of memory.
  798. Previous releases of this manual stressed on using failure-driven
  799. loops in those cases that no information needed to be passed to the
  800. next iteration via arguments.  This to avoid large amounts of garbage.
  801. This is no longer strictly necessary, but it should be noticed that
  802. garbage collection is a time consuming activity.  Failure driven loops
  803. tend to be faster for this reason.
  804. \section{Syntax Notes}
  805. SWI-Prolog uses standard `Edinburgh' syntax. A description of this
  806. syntax can be found in the Prolog books referenced in the introduction.
  807. Below are some non-standard or non-common constructs that are accepted
  808. by SWI-Prolog:
  809. \begin{itemlist}
  810.     \item [\exam{0'<char>}]
  811. This construct is not accepted by all Prolog systems that claim to have
  812. Edinburgh compatible syntax. It describes the ASCII value of <char>.
  813. To test whether \chr{C} is a lower case character one can use
  814. \exam{between(0'a, 0'z, C)}.
  815.     \item [\exam{/* \ldots /* \ldots */ \ldots */}]
  816. The \exam{/* \ldots */} comment statement can be nested. This is useful
  817. if some code with \exam{/* \ldots */} comment statements in it should be
  818. commented out.
  819. \end{itemlist}
  820. \subsection{ISO Syntax Support}
  821. SWI-Prolog offers ISO compatible extensions to the Edinburgh syntax.
  822. \subsubsection{Character Escape Syntax} \label{sec:charescapes}
  823. Within quoted atoms (using single quotes: \exam{'<atom>'} special
  824. characters are represented using escape-sequences.  An escape sequence
  825. is lead in by the backslash (\chr{\}) character. The list of
  826. escape sequences is compatible with the ISO standard, but contains one
  827. extension and the interpretation of numerically specified characters is
  828. slightly more flexible to improve compatibility.
  829. \begin{description}
  830.     \escapeitem{a}
  831. Alert character.  Normally the ASCII character 7 (beep).
  832.     \escapeitem{b}
  833. Backspace character.
  834.     \escapeitem{c}
  835. No output.  All input characters upto but not including the first
  836. non-layout character are skipped.  This allows for the specification
  837. of pretty-looking long lines.  For compatibility with Quintus Prolog.
  838. Nor supported by ISO.  Example:
  839. \begin{code}
  840. format('This is a long line that would look better if it was \c
  841.        split across multiple physical lines in the input')
  842. \end{code}
  843.     \escapeitem{\bnfmeta{{\sc RETURN}}}
  844. No output.  Skips input till the next non-layout character or to the
  845. end of the next line.  Same intention as \fmtseq{\c} but ISO compatible.
  846.     \escapeitem{f}
  847. Form-feed character.
  848.     \escapeitem{n}
  849. Next-line character.
  850.     \escapeitem{r}
  851. Carriage-return only (i.e.\ go back to the start of the line).
  852.     \escapeitem{t}
  853. Horizontal tab-character.
  854.     \escapeitem{v}
  855. Vertical tab-character (ASCII 11).
  856.     \escapeitem{x23}
  857. Hexadecimal specification of a character. \verb$23$ is just an example.
  858. The `x' may be followed by a maximum of 2 hexadecimal digits.  The
  859. closing \verb$\$ is optional.  The code \verb$\xa\3$ emits the character
  860. 10 (hexadecimal `a') followed by `3'.  The code \verb$\x201$ emits
  861. 32 (hexadecimal `20') followed by `1'.  According to ISO, the closing
  862. \verb$\$ is obligatory and the number of digits is unlimited.  The
  863. SWI-Prolog definition allows for ISO compatible specification, but
  864. is compatible with other implementations.
  865.     \escapeitem{40}
  866. Octal character specification.  The rules and remarks for hexadecimal
  867. specifications apply to octal specifications too, but the maximum
  868. allowed number of octal digits is 3.
  869.     \escapeitem{<character>}
  870. Any character immediately preceded by a \chr{\} is copied verbatim.
  871. Thus, \verb$'\\'$ is an atom consisting of a single \chr{\} and
  872. \verb$'\''$ and \verb$''''$ both describe the atom with a
  873. single~\verb$'$.
  874. \end{description}
  875. Character escaping is only available if the
  876. \exam{feature(character_escapes, true)} is active (default). See
  877. feature/2. Character escapes conflict with writef/2 in two ways:
  878. \verb$\40$ is interpreted as decimal 40 by writef/2, but character
  879. escapes handling by read has already interpreted as 32 (40 octal). Also,
  880. \fmtseq{\l} is translated to a single `l'. Double the \chr{\}
  881. (e.g.\ \fmtseq{\\}, use the above escape sequences or use format/2.
  882. \subsubsection{Syntax for Non-Decimal Numbers}
  883. SWI-Prolog implements both Edinburgh and ISO representations for
  884. non-decimal numbers. According to Edinburgh syntax, such numbers are
  885. written as \exam{<radix>'<number>}, where <radix> is a number between 2
  886. and 36. ISO defines binary, octal and hexadecimal numbers using
  887. \exam{0{\em [bxo]}<number>}. For example: \verb$A is 0b100 \/ 0xf00$ is
  888. a valid expression. Such numbers are always unsigned.
  889. \section{System Limits}
  890. \subsection{Limits on Memory Areas}
  891. SWI-Prolog has a number of memory areas which are only enlarged to a
  892. certain limit. The default sizes for these areas should suffice for most
  893. applications, but big applications may require larger ones. They are
  894. modified by command line options. The table below shows these areas. The
  895. first column gives the option name to modify the size of the area. The
  896. option character is immediately followed by a number and optionally by a
  897. \const{k} or \const{m}. With \const{k} or no unit indicator, the value
  898. is interpreted in Kbytes (1024 bytes), with \const{m}, the value is
  899. interpreted in Mbytes ($1024 \times 1024$ bytes).
  900. The local-, global- and trail-stack are limited to 64 Mbytes on 32 bit
  901. processors, or more in general to $\pow{2}{\mbox{bits-per-long} - 6}$
  902. bytes.
  903. \begin{table}
  904. \begin{center}
  905. \begin{tabular}{|c|c|l|p{5cm}|}
  906. \hline
  907. Option & Default & Area name & Description \\
  908. \hline
  909. \cmdlineoption{-L} & 2M & \bf local stack & The local stack is used to store
  910.                        the execution environments of procedure
  911.                        invocations. The space for an environment is
  912.                        reclaimed when it fails, exits without leaving
  913.                        choice points, the alternatives are cut of with
  914.                        the !/0 predicate or no choice points have
  915.                        been created since the invocation and the last
  916.                        subclause is started (tail recursion optimisation). \\
  917. \cmdlineoption{-G} & 4M & \bf global stack & The global stack is used
  918.                        to store terms created during Prolog's
  919.                        execution. Terms on this stack will be reclaimed
  920.                        by backtracking to a point before the term
  921.                        was created or by garbage collection (provided the
  922.                        term is no longer referenced). \\
  923. \cmdlineoption{-T} & 4M & \bf trail stack & The trail stack is used to store
  924.                        assignments during execution.  Entries on this
  925.                        stack remain alive until backtracking before the
  926.                        point of creation or the garbage collector 
  927.                        determines they are nor needed any longer. \\
  928. \cmdlineoption{-A} & 1M & \bf argument stack & The argument stack is used to
  929.                        store one of the intermediate code interpreter's 
  930.                        registers. The amount of space needed on this
  931.                        stack is determined entirely by the depth in
  932.                        which terms are nested in the clauses that
  933.                        constitute the program. Overflow is most likely
  934.                        when using long strings in a clause. \\
  935. \hline
  936. \end{tabular}
  937. \end{center}
  938.     \caption{Memory areas}
  939.     \label{tab:areas}
  940. \end{table}
  941. \subsubsection{The heap}                \label{sec:heap}
  942. \index{stack,memory management}%
  943. \index{memory,layout}%
  944. With the heap, we refer to the memory area used by \funcref{malloc}{}
  945. and friends. SWI-Prolog uses the area to store atoms, functors,
  946. predicates and their clauses, records and other dynamic data.  As of
  947. SWI-Prolog 2.8.5, no limits are imposed on the addresses returned by
  948. \funcref{malloc}{} and friends.
  949. On some machines, the runtime stacks described above are allocated using
  950. `sparse allocation'. Virtual space upto the limit is claimed at startup
  951. and committed and released while the area grows and shrinks.  On Win32
  952. platform this is realised using \funcref{VirtualAlloc}{} and friends.
  953. On Unix systems this is realised using \funcref{mmap}{}, either mapping
  954. \file{/dev/zero} or a temporary file created in \file{/tmp}.
  955. As Unix provides no way to reserve part of the address space, this
  956. process may lead to conflicts. By default, SWI-Prolog computes the
  957. required virtual address space for the runtime stacks. If available, it
  958. uses \funcref{getrlimit}{} to determine the top of the virtual space
  959. reserved for \funcref{malloc}{} usage and locates the stacks in the top
  960. of this area. It assumes no other \funcref{mmap}{} activity such as
  961. mapping shared libraries or \funcref{mmap}{} by foreign modules will use
  962. the area reserved for the heap and \funcref{malloc}{},
  963. \funcref{malloc}{} will grow the heap from low to high addresses and
  964. will notice the existence of the Prolog stacks.
  965. These assumptions appear to hold. The user may using the
  966. \cmdlineoption{-H} to specify the maximum heap-size.  In this case,
  967. the Prolog stacks will be allocated at the indicated size from the
  968. current top of the heap.  On these system, statistics/[0,2] reports
  969. \idx{heaplimit} and \idx{heap}.  The \idx{heaplimit} value is the
  970. distance between the {\em break} and the first Prolog stack. 
  971. The \idx{heap} value is the distance between what Prolog assumes to
  972. be the base of the heap%
  973.     \footnote{There is no portable and reliable way to find the real
  974.           base.}
  975. and the current location of the break.
  976. \subsection{Other Limits}
  977. \begin{description}
  978.     \item[Clauses]
  979. Currently the following limitations apply to clauses.  The arity may
  980. not be more than 1024 and the number of variables should be less than
  981. 65536.
  982.     \item[Atoms and Strings]
  983. SWI-Prolog has no limits on the sizes of atoms and strings.  read/1 and
  984. its derivatives however normally limit the number of newlines in an atom
  985. or string to 5 to improve error detection and recovery.  This can be
  986. switched off with style_check/1.
  987.     \item[Address space]
  988. SWI-Prolog data is packed in a 32-bit word, which contains both type
  989. and value information.  The size of the various memory areas is limited
  990. to 128 Mb for each of the areas.  With some redesign, the program area
  991. could be split into data that should be within this range and the rest
  992. of the data, virtually unlimiting the program size.
  993.     \item[Integers]
  994. Integers are 32-bit to the user, but integers upto the value of the
  995. {\tt max_tagged_integer} feature are represented more efficiently.
  996.     \item[Floats]
  997. Floating point numbers are represented as native double precision
  998. floats, 64 bit IEEE on most machines.
  999. \end{description}
  1000. \subsection{Reserved Names}
  1001. The boot compiler (see \cmdlineoption{-b} option) does not support the module
  1002. system (yet).  As large parts of the system are written in Prolog itself
  1003. we need some way to avoid name clashes with the user's predicates, 
  1004. database keys, etc.  Like Edinburgh C-Prolog \cite{CPROLOG:manual} all
  1005. predicates, database keys, etc.\ that should be hidden from the user
  1006. start with a dollar (\chr{\$}) sign (see style_check/1).
  1007. The compiler uses the special functor \const{\$VAR\$/1} while analysing the
  1008. clause to compile.  Using this functor in a program causes unpredictable
  1009. behaviour of the compiler and resulting program.
  1010.